package com.example.javamaday.leetcode.sort;

public class LcSort0912_03 {

    public static void main(String[] args) {

    }

    // 选择排序
    public int[] sortArray(int[] nums) {
        if (nums.length <= 1) {
            return nums;
        }
        for (int i = 0; i < nums.length; i++) {
            // 一次遍历，找到最小值，和i位置的数据交换
            int minIndex = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[minIndex] > nums[j]){
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                exchange(nums, i, minIndex);
            }
        }
        return nums;
    }

    public void exchange(int[] nums, int a, int b) {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
}
